<?php
//
// +---------------------------------------------------------------------------------+
// | Package: EezeeSite Core                                                         |
// +---------------------------------------------------------------------------------+
// | Copyright (C) 2006  EezeeSite Development Team                                  |
// +---------------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or                   |
// | modify it under the terms of the GNU General Public License                     |
// | as published by the Free Software Foundation; either version 2                  |
// | of the License, or (at your option) any later version.                          |
// |                                                                                 |
// | This program is distributed in the hope that it will be useful,                 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                  |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                   |
// | GNU General Public License for more details.                                    |
// |                                                                                 |
// | You should have received a copy of the GNU General Public License               |
// | along with this program; if not, write to the Free Software                     |
// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. |
// +---------------------------------------------------------------------------------+
// | Author: Mayukh De <mayukhde@gmail.com>                                          |
// +---------------------------------------------------------------------------------+
//

class ESController
{
    var $modes = array(
                      'INDEX' => array('register', 'user', 'static', 'content', 'my'),
                      'ADMIN' => array(),
                      'ERROR' => array('404')
                      );
    var $patterns = array(
                         'REGISTER'   => array(array('register', '?mode=register'), array(array(''), array(''))),
                         'STATIC'     => array(array('%s.html', '?mode=static&id=%s'), array(array('([A-Za-z0-9_]+)'), array('$1'))),
                         'USERHOME'   => array(array('user/%s', '?mode=user&user=%s'), array(array('([A-Za-z0-9_]+)'), array('$1'))),
                         'USERACTION' => array(array('user/%s/%s', '?mode=user&user=%s&action=%s'), array(array('([A-Za-z0-9_]+)', '([a-z_]+)'), array('$1', '$2'))),
                         'USERPAGE'   => array(array('user/%s/user_pages/%s', '?mode=user&user=%s&action=user_pages&page=%s'), array(array('([A-Za-z0-9_]+)', '([A-Za-z0-9_]+)'), array('$1', '$2'))),
                         'MYHOME'     => array(array('?mode=my', '?mode=my')),
                         'ADMINHOME'  => array(array('admin%s', 'admin%s'))
                         );
                         
    function getURLRewriteRules()
    {
        $needsRewriting = array('REGISTER', 'STATIC', 'USERPAGE', 'USERACTION', 'USERHOME');
        $str = array();
        for ($i = 0; $i < count($needsRewriting); $i++) {
            if (array_key_exists($needsRewriting[$i], $this->patterns)) {
                $left = vsprintf($this->patterns[$needsRewriting[$i]][0][0], $this->patterns[$needsRewriting[$i]][1][0]);
                $right = vsprintf($this->patterns[$needsRewriting[$i]][0][1], $this->patterns[$needsRewriting[$i]][1][1]);
                $str[$i] = "RewriteRule ^".$left." ".$right." [L]";
            }
        }
        $str = implode("\n", $str);
        return "\n".$str."\n";
    }
    
    function getIncFileToLoad()
    {
        global $ESResponse;
        $s = explode(ES_FILE_EXTN, $_SERVER['SCRIPT_NAME']);
        $script = substr($s[0], 1);
        $_GET['mode'] = (isset($_GET['mode'])) ? $_GET['mode'] : 'home';
        if (array_key_exists(strtoupper($script), $this->modes)) {
            if (in_array($_GET['mode'], $this->modes[strtoupper($script)]) || $_GET['mode'] == 'home') {
                $inc = $GLOBALS['ES']['INCDIR'].'/'.$script.'_mode_'.$_GET['mode'].'.inc.php';
                if (file_exists($inc)) {
                    return $inc;
                } else {
                    $ESResponse->raiseESError('A valid inc file "'.$script.'_mode_'.$_GET['mode'].'.inc.php" was not found in its container', 'email');
                }
            } else {
                $ESResponse->raiseESError('Hacking attempt?! Unknown mode requested: '.$_SERVER['REQUEST_URI'], 'log');
            }
        } else {
            $ESResponse->raiseESError('Script file "'.$script.'" does not exist in core configuration but sends mode "'.$_GET['mode'].'" requests!', 'email');
        }
        $ESResponse->sendHTTPHeader('404');
        return $GLOBALS['ES']['INCDIR'].'/error_mode_404.inc.php';
    }
    
    function getLink($anchor, $element = null, $data = null, $attributes = null, $name = null)
    {
        $attributes = ($attributes && is_array($attributes)) ? $attributes : null;
        $data = ($data && is_array($data)) ? $data : array();
        $att = '';
        if ($attributes) {
            foreach ($attributes as $k => $v) {
                $att.= ' '.strtolower($k).'="'.$v.'"';
            }
        }
        if ($element && array_key_exists($element, $this->patterns)) {
            $ret = ($GLOBALS['ES']['CLIENTCONF']['rewrite_url'] && $GLOBALS['ES']['CLIENTCONF']['rewrite_url'] = '1' || $GLOBALS['ES']['CLIENTCONF']['rewrite_url'] = 1) ? 0 : 1;
            $href = ES_BASE_URL.vsprintf($this->patterns[$element][0][$ret], $data);
        } else {
            $href = '#';
        }
        $href.= $name;
        $openTag = '<a href="'.$href.'"';
        $openTag.= ($att != '') ? $att.'>' : '>';
        $closeTag = '</a>';
        $link = $openTag.$anchor.$closeTag;
        return $link;
    }
}

?>